package com.leon.datalink.rule.handler.impl;

import akka.actor.ActorRef;
import akka.actor.Props;
import com.leon.datalink.core.persistence.PersistenceConfigManager;
import com.leon.datalink.rule.persistence.PersistentActorWrapper;
import com.leon.datalink.node.NodeActorCreateParam;
import com.leon.datalink.node.NodeActorCreator;
import com.leon.datalink.node.constans.NodeActorEnum;
import com.leon.datalink.node.graph.GraphNode;
import com.leon.datalink.node.graph.NodeData;
import com.leon.datalink.rule.handler.AbstractRuleCreateHandler;

/**
 * 单机模式下规则启动处理
 */
public class SingleRuleCreateHandler extends AbstractRuleCreateHandler {

    @Override
    protected ActorRef createActor(GraphNode graphNode,NodeActorCreateParam createParam) {
        NodeData nodeData = graphNode.getData();
        NodeActorEnum nodeActorEnum = NodeActorEnum.valueOf(nodeData.getCode());

        // 检查是否需要持久化
        if (PersistenceConfigManager.getInstance().shouldPersist(createParam.getNodeId(), nodeActorEnum.getNodeActor().getSimpleName(), createParam.getProperties())) {
            // 创建持久化包装器
            Props props = Props.create(PersistentActorWrapper.class, nodeActorEnum.getNodeActor(), createParam);
            return context.actorOf(props, graphNode.getId());
        } else {
            // 原有逻辑不变
            Props props = Props.create(new NodeActorCreator(nodeActorEnum.getNodeActor(), createParam));
            return context.actorOf(props, graphNode.getId());
        }
    }

}
